package com.enzo.moredb.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger文档配置
 *
 * @author Enzo
 * @date 2020-5-21
 */
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    /**
     * 初始化api文档
     *
     * @return springfox.documentation.spring.web.plugins.Docket
     * @author Enzo
     * @date 2020-5-21
     **/
    @Bean
    public Docket createRestApi() {
        // 定义全局header参数
        List<Parameter> pars = new ArrayList<>();
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                //如果不想将所有的接口都通过swagger管理的话，可以将RequestHandlerSelectors.any()修改为RequestHandlerSelectors.basePackage()
                .apis(RequestHandlerSelectors.any())
//                .apis(RequestHandlerSelectors.basePackage("com.dc.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo()).securitySchemes(securitySchemes()).securityContexts(securityContexts());
    }

    /**
     * 全局认证参数key
     *
     * @param
     * @return java.util.List<springfox.documentation.service.ApiKey>
     * @author Enzo
     * @date 2020-7-7
     **/
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("authToken", "authToken", "header"));
        return apiKeyList;
    }

    /**
     * 全局认证参数默认配置
     *
     * @param
     * @return java.util.List<springfox.documentation.service.SecurityReference>
     * @author Enzo
     * @date 2020-7-7
     **/
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("authToken", authorizationScopes));
        return securityReferences;
    }

    /**
     * 全局认证参数内容
     *
     * @param
     * @return java.util.List<springfox.documentation.spi.service.contexts.SecurityContext>
     * @author Enzo
     * @date 2020-7-7
     **/
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!login).*$"))
                        .build());
        return securityContexts;
    }


    /**
     * 设置文档描述信息
     *
     * @return springfox.documentation.service.ApiInfo
     * @author Enzo
     * @date 2020-5-21
     **/
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("基础信息模块API")
                .description("Rest API接口")
                .version("1.0").build();
    }
}
